﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.IO;

namespace LuckyBingoRcon.Matches
{
	/// <summary>
    /// Known map names:
    /// Array;mp_array;ar
    /// Havana;mp_cairo;hv
    /// Launch;mp_cosmodrome;la
    /// Cracked;mp_cracked;ca
    /// Crisis;mp_crisis;ci
    /// Grid;mp_duga;gr
    /// Firing Range;mp_firingrange;fr
    /// Hanoi;mp_hanoi;hn
    /// Jungle;mp_havoc;ju
    /// Summit;mp_mountain;su
    /// Nuke Town;mp_nuked;nt
    /// Radiation;mp_radiation;ra
    /// WMD;mp_russianbase;wm
    /// Villa;mp_villa;vi
	/// </summary>
	public class MapLookup
	{
		private Dictionary<string, string> pretty2Ugly = new Dictionary<string, string>();
		private Dictionary<string, string> ugly2Pretty = new Dictionary<string, string>();
        private Dictionary<string, string> ugly2Abbrev = new Dictionary<string, string>();
        private Dictionary<string, string> abbrev2ugly = new Dictionary<string, string>();

		public MapLookup()
		{
			FileInfo mapFile = new FileInfo("Configs\\mapList.cfg");
            if(mapFile.Exists)                  // Found the Map List Config file, load from there
            {
                string[] configStrings = File.ReadAllLines(mapFile.FullName);
                string[] args;
                foreach(string entry in configStrings)
                {
                    if(entry.StartsWith("//"))
                        continue;
                    args = Regex.Split(entry, ";");
                    if (!args.Length.Equals(3))
                        throw new Exception("Map list configuration file has erroneous entires!");
                    pretty2Ugly.Add(args[0], args[1]);
                    ugly2Abbrev.Add(args[1], args[2]);
                }
            }
            else                                // Can't find Map List Config file, use map defaults (Maps as of 9 November 2010)
            {
                //RconForm.Console.WriteLine("Could not find file: <Root>\\Configs\\mapList.cfg");
                pretty2Ugly.Add("Array",		    "mp_array");
			    pretty2Ugly.Add("Havana",		    "mp_cairo");
			    pretty2Ugly.Add("Launch",		    "mp_cosmodrome");
			    pretty2Ugly.Add("Cracked",		    "mp_cracked");
			    pretty2Ugly.Add("Crisis",		    "mp_crisis");
			    pretty2Ugly.Add("Grid",			    "mp_duga");
			    pretty2Ugly.Add("Firing Range",     "mp_firingrange");
			    pretty2Ugly.Add("Hanoi",		    "mp_hanoi");
			    pretty2Ugly.Add("Jungle",		    "mp_havoc");
			    pretty2Ugly.Add("Summit",		    "mp_mountain");
			    pretty2Ugly.Add("Nuke Town",	    "mp_nuked");
			    pretty2Ugly.Add("Radiation",	    "mp_radiation");
			    pretty2Ugly.Add("WMD",			    "mp_russianbase");
			    pretty2Ugly.Add("Villa",		    "mp_villa");
                ugly2Abbrev.Add("mp_array",         "ar");
                ugly2Abbrev.Add("mp_cairo",         "hv");
                ugly2Abbrev.Add("mp_cosmodrome",    "la");
                ugly2Abbrev.Add("mp_cracked",       "ca");
                ugly2Abbrev.Add("mp_crisis",        "ci");
                ugly2Abbrev.Add("mp_duga",          "gr");
                ugly2Abbrev.Add("mp_firingrange",   "fr");
                ugly2Abbrev.Add("mp_hanoi",         "hn");
                ugly2Abbrev.Add("mp_havoc",         "ju");
                ugly2Abbrev.Add("mp_mountain",      "su");
                ugly2Abbrev.Add("mp_nuked",         "nt");
                ugly2Abbrev.Add("mp_radiation",     "ra");
                ugly2Abbrev.Add("mp_russianbase",   "wm");
                ugly2Abbrev.Add("mp_villa",         "vi");
            }
			foreach (string key in pretty2Ugly.Keys)
			{
				ugly2Pretty.Add(pretty2Ugly[key], key);
			}
            foreach (string key in ugly2Abbrev.Keys)
            {
                abbrev2ugly.Add(ugly2Abbrev[key], key);
            }
		}

		/// <summary>
		/// NOT IMPLEMENTED, haven't decided on a file format
		/// </summary>
		/// <param name="mapFile"></param>
		public MapLookup(string mapFile)
		{
			// not implemented
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="uglyName"></param>
		/// <returns>NULL if uglyName is not known</returns>
		public string GetPrettyName(string uglyName)
		{
			if (ugly2Pretty.ContainsKey(uglyName))
			{
				return ugly2Pretty[uglyName];
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="prettyName"></param>
		/// <returns>NULL if prettyName is not known</returns>
		public string GetUglyName(string prettyName)
		{
			if (pretty2Ugly.ContainsKey(prettyName))
			{
                return pretty2Ugly[prettyName];
			}
			else
			{
				return null;
			}
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="uglyName"></param>
        /// <returns>NULL if abbrev is not known</returns>
        public string GetAbbreviation(string uglyName)
        {
            if (ugly2Abbrev.ContainsKey(uglyName))
            {
                return ugly2Abbrev[uglyName];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="abbrev"></param>
        /// <returns>NULL if UglyName is not known</returns>
        public string GetUglyFromAbbrev(string abbrev)
        {
            if (abbrev2ugly.ContainsKey(abbrev))
            {
                return abbrev2ugly[abbrev];
            }
            else
            {
                return null;
            }
        }

        public bool IsValidName(string name)
        {
            if (name.Equals("Any"))
                return true;
            return (pretty2Ugly.ContainsKey(name) || ugly2Pretty.ContainsKey(name));
        }

        public string[] GetPrettyNames() 
        { 
            string[] sa = new string[pretty2Ugly.Keys.Count]; 
            pretty2Ugly.Keys.CopyTo(sa, 0); 
            return sa; 
        }    
        
        public string[] GetUglyNames() 
        { 
            string[] sa = new string[ugly2Pretty.Keys.Count]; 
            ugly2Pretty.Keys.CopyTo(sa, 0); 
            return sa; 
        }  
	}
}
